package cn.fufeiii.bt.common.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * BeanCopyUtil
 * 基于spring-BeanUtils
 *
 * @author FuFei
 */
public final class BeanCopyUtil {

    public static <T> T copy(Object source, Class<T> targetClazz) {
        T newInstance;
        try {
            newInstance = targetClazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        BeanUtils.copyProperties(source, newInstance);
        return newInstance;
    }

    public static <T> List<T> copyList(List<?> sourceList, Class<T> targetClazz) {
        List<T> list = new ArrayList<>();
        for (Object source : sourceList) {
            list.add(copy(source, targetClazz));
        }
        return list;
    }

}
